package com;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.devplatform.util.OssUtil;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class test {
    private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    //获取密码加密后的密文
    @Test
    public void test() {
        System.out.println(bCryptPasswordEncoder.encode("1234567a"));
    }

    //查看文件
    @Test
    public void test1() {
        OssUtil.getFiles();
    }

    //上传文件
    @Test
    public void test3() throws IOException {
        String src = "D:\\javaCode\\DevPlatform\\img\\defaultBackground.png";
        File f = new File(src);
        if (!f.exists()) {
            return;
        }
        FileInputStream fis = new FileInputStream(f);
        OssUtil.adminUploadFile(fis, "defaultBackground.png");
    }

    //删除文件
    @Test
    public void test2() {
        OssUtil.adminDeleteFile("2025-09-28_02:11:141.jpg");
        OssUtil.adminDeleteFile("1971552782557167618/2025-09-29_20:53:35defaultAvatar.jpg");
        OssUtil.getFiles();
    }

    // 初始化OSS客户端
    private static OSS getOssClient() {
        String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 替换为你的Bucket地域节点
        String accessKeyId = "LTAI5tLecQvLP8EyQ2XRVCsy"; // 替换为你的AccessKeyId
        String accessKeySecret = "XXQrtvl2imXqcUVSS936KuK81w7hHu"; // 替换为你的AccessKeySecret
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    private static void clearBucket(OSS ossClient, String bucketName) {
        // 1. 删除所有文件（使用setter设置marker）
        String marker = null;
        do {
            ListObjectsRequest request = new ListObjectsRequest(bucketName);
            request.setMarker(marker); // 用setMarker替代withMarker

            ObjectListing objectListing = ossClient.listObjects(request);
            List<OSSObjectSummary> objectSummaries = objectListing.getObjectSummaries();

            if (objectSummaries.isEmpty()) {
                break;
            }

            // 批量删除文件
            List<String> objectKeys = objectSummaries.stream()
                    .map(OSSObjectSummary::getKey)
                    .toList();
            ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectKeys));

            if (!objectListing.isTruncated()) {
                break;
            }
            marker = objectListing.getNextMarker();
        } while (true);

        // 2. 终止分片上传（使用setter设置标记）
        String keyMarker = null;
        String uploadIdMarker = null;
        do {
            ListMultipartUploadsRequest request = new ListMultipartUploadsRequest(bucketName);
            // 用setter替代withKeyMarker和withUploadIdMarker
            request.setKeyMarker(keyMarker);
            request.setUploadIdMarker(uploadIdMarker);

            MultipartUploadListing listing = ossClient.listMultipartUploads(request);
            List<MultipartUpload> uploads = listing.getMultipartUploads();

            for (MultipartUpload upload : uploads) {
                AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(
                        bucketName, upload.getKey(), upload.getUploadId()
                );
                ossClient.abortMultipartUpload(abortRequest);
            }

            if (!listing.isTruncated()) {
                break;
            }
            keyMarker = listing.getNextKeyMarker();
            uploadIdMarker = listing.getNextUploadIdMarker();
        } while (true);
    }

    public static void deleteBucket(String bucketName) {
        OSS ossClient = getOssClient();
        try {
            if (!ossClient.doesBucketExist(bucketName)) {
                System.out.println("Bucket不存在：" + bucketName);
                return;
            }

            clearBucket(ossClient, bucketName);
            ossClient.deleteBucket(bucketName);
            System.out.println("Bucket删除成功：" + bucketName);
        } catch (Exception e) {
            System.err.println("删除失败：" + e.getMessage());
        } finally {
            ossClient.shutdown();
        }
    }

    @Test
    public void deleteAll() {
        String bucketName = "seecen-devplatform"; // 替换为你的Bucket名称
        deleteBucket(bucketName);
    }
}
